package com.senmiao.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.senmiao.ssm.entity.Employee;
import com.senmiao.ssm.service.AccessControlService;
import com.senmiao.ssm.service.EmployeeService;
import com.senmiao.ssm.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Reference(check = false)
    private EmployeeService employeeService;

    @Autowired
    private AccessControlService accessControlService;

    /**
     * 根据id获取员工信息
     *
     * @param id
     * @return
     */
//    @RequiresPermissions({"emp:get"})
    @GetMapping("/getById")
    public Object getById(Integer id) {
        Employee employee = null;
        Subject subject = SecurityUtils.getSubject();
        System.out.println("认证状态："+subject.isAuthenticated());
        if (subject.isPermitted("emp:get")) {
            employee = employeeService.getById(id);
        }
        return employee;
    }

    /**
     * 根据页码号获取员工信息
     *
     * @param pageNum 页码号
     * @return 员工信息及分页信息
     */
    @RequiresPermissions({"emp:get"})
    @GetMapping("/list")
    public Object getEmp(Integer pageNum) {
        return employeeService.getEmployees(pageNum);
    }

    /**
     * 修改员工信息
     *
     * @param employee
     * @return
     */
    @RequiresPermissions({"emp:update"})
    @PostMapping("/update")
    public Object updateEmp(Employee employee) {
        return employeeService.updateEmp(employee);
    }

    /**
     * 新增员工信息
     *
     * @param employee
     * @return
     */
    @RequiresPermissions({"emp:add"})
    @PostMapping("/add")
    public Object addEmp(Employee employee) {
        int result = 0;
        if (!StringUtils.isEmpty(employee.getName()) && employee.getAge() != null && employee.getSex() != null) {
            result = employeeService.add(employee);
        }
        return result;
    }

    /**
     * 删除员工
     *
     * @param request 请求对象
     * @return
     */
    @RequiresPermissions({"emp:delete"})
    @PostMapping("/delete")
    public Object deleteEmpByIds(HttpServletRequest request) {
        int result = 0;
        String[] idsStr = request.getParameterValues("id");
        List<Integer> ids = new ArrayList<>();
        if (idsStr != null && idsStr.length > 0) {
            for (String s : idsStr) {
                if (s != null) {
                    ids.add(Integer.valueOf(s));
                }
            }
            if (ids.size() > 0) {
//            调用服务层
                result = employeeService.deleteEmp(ids);
            }
        }
        return result;
    }

    @GetMapping("/login")
    public Result login(String username, String password) {
        Result<Object> result = new Result<>();
//        accessControlService.login(username, password);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        subject.login(token);
        return result.ok().msg("登录成功");
    }

}